import os
import time
import datetime
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from clickhouse_driver import Client

#功能：检查当天dt分区是否存在

# 中文字体支持
mpl.rcParams['font.family'] = 'STKAITI'  # 'STKAITI'——字体

#适合查询，写入用client
client = Client(host="192.168.50.40",  user="default", password="9defbcg", port="9090", database="ods")

#涨停分析
def query(sql, **kwargs):
    # print("Query:" + sql)
    result = client.execute(sql)
    #支持设置列
    columns = None
    if kwargs.get("columns") != None:
        columns = kwargs.get("columns")
    df = pd.DataFrame(result, columns=columns)
    #save_to
    save_to = kwargs.get("save_to")
    if save_to != None:
        save_to = os.path.join(save_to, "QUERY_{}.csv".format(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
        print(save_to)
        df.to_csv(save_to, encoding="utf_8_sig", index=False)  # index设置为False，就不会有第一列的序号
    return df


def main():
    print("start....")
    #base数据检查
    tables = ['ods_stock_base', 'ods_stock_detail_df', 'ods_stock_trade_day_df',
              'ods_stock_holder_jka', 'ods_stock_hot_df', 'ods_stock_finance_report_jka',
              'ods_stock_zjlr_df', 'ods_stock_lift_ban_df'
              ]

    #遍历检查当天分区是否存在
    for table in tables:
        df = query("select dt ,count(0) from "+ table +" WHERE dt>'20230820' group by dt order by dt desc;")
        check_dt(table, df)


def check_dt(table, df, **kwargs):
    dt = None
    if kwargs.get("dt") != None:
        dt = kwargs.get("dt")
    else:
        dt = datetime.datetime.now().strftime('%Y%m%d')
    # print(df.head())
    if len(df) == 0:
        print('[%s]表检测失败，没有数据>>>>>>>>>>>>>>>>>>>>>>>>>' % (table))
        return

    #检查dt是否存在，dt在第0列
    dfToday = df[df[0]>=dt]

    if len(dfToday) == 1:
        print('[%s]表检测通过，分区%s存在！' %(table, dt))
        #输出数据量
        print(df.head())
    else:
        print('[%s]表检测失败，分区%s不存在！>>>>>>>>>>>>>>>>>>>>>>>>>' %(table, dt))

if __name__ == '__main__':
    main()



